$(document).ready(function(){ 
	toLoad();
}); 
function toLoad(){
	var dataUrl="./diseaseHistory/toDisHisList"
	 $.ajax({
		　　url : dataUrl,
		　　type : "POST",
	       dataType: 'json',
	       cache: false,
		　　success : function(data) {
			console.log(data);
			if(data!=null){
				var baseHTML="";
				var imgHTML="";
				var sysHTML="";
				$("#basehistory").html("");
				$("#imghistory").html("");
				$("#inquiryhistory").html("");
				if(data.list!=null&&data.list.length>0){
					baseHTML+="<a href=\"javascript:;\" class=\"weui_btn weui_btn_mini weui_btn_default\"  id=\"basemagr\" style=\"margin-left:5px;margin-top:5px;\" onclick=\"magrBase();\">管理</a>";
					for(var i=0;i<data.list.length;i++){
						var mydata=data.list[i];
						baseHTML+="<a class=\"weui_cell baseclass\" href=\"javascript:;\" onclick=\"toCaseHistory('"+mydata.id+"');\">";
						baseHTML+=" <div class=\"weui_cell_hd\"><img src=\"\"  style=\"width:20px;margin-right:5px;display:block\"></div>";
						baseHTML+="<div class=\"weui_cell_bd weui_cell_primary\">";
						baseHTML+="<p style=\"font-size: 12px;\">"+mydata.historyName+"</p>";
						baseHTML+="</div>";
						baseHTML+="<div class=\"weui_cell_ft\" style=\"font-size: 12px;\">[基础病历]</div>";
						baseHTML+="  <i class=\"weui_icon_cancel\" style=\"position:absolute;right:-32px;top:5px;\" onclick=\"delBaseHis('"+mydata.id+"');\"></i></a> ";
					}
				
				}else{
					baseHTML+="<a href=\"javascript:;\" class=\"weui_btn weui_btn_mini weui_btn_default\" id=\"basemagr\" style=\"margin-left:5px;margin-top:5px;\" onclick=\"magrBase();\">管理</a>";
					baseHTML+="<span style=\"font-size: 11px;text-align: center;\">暂无基础病历</span>";
				}
				$("#basehistory").append(baseHTML);
				if(data.imgList!=null&&data.imgList.length>0){
					imgHTML+="<a href=\"javascript:;\" class=\"weui_btn weui_btn_mini weui_btn_default\" style=\"margin-left:5px;margin-top:5px;\" onclick=\"addPic();\">新增</a>";
					imgHTML+="<a href=\"javascript:;\" class=\"weui_btn weui_btn_mini weui_btn_default\"  id=\"picmagr\" style=\"margin-left:5px;margin-top:5px;\" onclick=\"magrpic();\">管理</a>";
					for(var i=0;i<data.imgList.length;i++){
						var imgdata=data.imgList[i];
						imgHTML+="<a class=\"weui_cell picclass\" href=\"javascript:;\" style=\"position: relative;\" onclick=\"toImgCaseHistory('"+imgdata.id+"');\">";
						imgHTML+=" <div class=\"weui_cell_hd\"><img src=\"\" style=\"width:20px;margin-right:5px;display:block\"></div>";
						imgHTML+=" <div class=\"weui_cell_bd weui_cell_primary\">";
						imgHTML+=" <p style=\"font-size: 12px;\">"+imgdata.historyName+"</p>";
						imgHTML+="</div>";
						imgHTML+=" <div class=\"weui_cell_ft\" style=\"font-size: 12px;\">[图片病历]</div>";
						imgHTML+=" <i class=\"weui_icon_cancel\" style=\"position:absolute;right:-32px;top:5px;\" onclick=\"delImgHis('"+imgdata.id+"');\"></i> </a>";
					}
					
				}else{
					imgHTML+="<a href=\"javascript:;\" class=\"weui_btn weui_btn_mini weui_btn_default\"  style=\"margin-left:5px;margin-top:5px;\" onclick=\"addPic();\">新增</a>";
					imgHTML+="<span style=\"font-size: 11px;text-align: center;\">暂无图片病历</span>";
				}
				$("#imghistory").append(imgHTML);
				if(data.sysList!=null&&data.sysList.length>0){
					sysHTML+="<a href=\"javascript:;\" class=\"weui_btn weui_btn_mini weui_btn_default\" id=\"sysmagr\"  style=\"margin-left:5px;margin-top:5px;\" onclick=\"magrSys();\">管理</a>";
					for(var i=0;i<data.sysList.length;i++){
						var sysdata=data.sysList[i];
						sysHTML+="<a class=\"weui_cell sysclass\" href=\"javascript:;\" style=\"position: relative;\" onclick=\"toSysCaseHistory('"+sysdata.id+"');\">";
						sysHTML+=" <div class=\"weui_cell_hd\"><img src=\"\" style=\"width:20px;margin-right:5px;display:block\"></div>";
						sysHTML+="<div class=\"weui_cell_bd weui_cell_primary\">";
						sysHTML+="<p style=\"font-size: 12px;\">"+sysdata.caseHistoryName+"</p>";
						sysHTML+="</div>";
						sysHTML+="<div class=\"weui_cell_ft\" style=\"font-size: 12px;\">[自诊病历]</div>";
						sysHTML+=" <i class=\"weui_icon_cancel\" style=\"position:absolute;right:-32px;top:5px;\" onclick=\"delSysHis('"+sysdata.id+"');\"></i></a> ";
					}
					
				}else{
					sysHTML+="<a href=\"javascript:;\" class=\"weui_btn weui_btn_mini weui_btn_default\" id=\"sysmagr\" style=\"margin-left:5px;margin-top:5px;\" onclick=\"magrSys();\">管理</a>";
					sysHTML+=" <span style=\"font-size: 11px;text-align: center;\">暂无自诊病历</span>";
				}
				$("#inquiryhistory").append(sysHTML);
				
			}
		　　},
		});
}
function magrSys(){
	if($("#sysmagr").text()=="管理"){
		$(".sysclass").stop().animate({ right: '50px' }, 200);
		$("#sysmagr").text("取消");
	}else{
		$(".sysclass").stop().animate({ right: '0px' }, 200);
		$("#sysmagr").text("管理");
	}

}
//基础病历
function magrBase(){
	if($("#basemagr").text()=="管理"){
		$(".baseclass").stop().animate({ right: '50px' }, 200);
		$("#basemagr").text("取消");
	}else{
		$(".baseclass").stop().animate({ right: '0px' }, 200);
		$("#basemagr").text("管理");
	}
	
}
function magrpic(){
	if($("#picmagr").text()=="管理"){
		$(".picclass").stop().animate({ right: '50px' }, 200);
		$("#picmagr").text("取消");
	}else{
		$(".picclass").stop().animate({ right: '0px' }, 200);
		$("#picmagr").text("管理");
	}
	
}

function delBaseHis(id){
	$(".baseclass").removeAttr("onclick");
	var url= "./diseaseHistory/delImgORBaseHis.spring";
	 $.ajax({
		　　url : url,
		　　data : {id:id},
		　　type : "POST",
		  dataType: 'json',
		　　success : function(data) {
			console.log(data.resultCode);
			if(data.resultCode=='1'){
				alert("删除成功");
				toLoad();
			}else{
			alert("删除失败，请稍后重试");
			toLoad();
			}
		　　}
		});
}
function delImgHis(id){
	$(".picclass").removeAttr("onclick");
	var url= "./diseaseHistory/delImgORBaseHis.spring";
	 $.ajax({
		　　url : url,
		　　data : {id:id},
		　　type : "POST",
		  dataType: 'json',
		　　success : function(data) {
			console.log(data.resultCode);
			if(data.resultCode=='1'){
				alert("删除成功");
				toLoad();
			}else{
			alert("删除失败，请稍后重试");
			toLoad();
			}
		　　}
		});
}

function delSysHis(id){
	$(".sysclass").removeAttr("onclick");
	var url= "./diseaseHistory/delSysHis.spring";
	 $.ajax({
		　　url : url,
		　　data : {id:id},
		　　type : "POST",
		  dataType: 'json',
		　　success : function(data) {
			console.log(data.resultCode);
			if(data.resultCode=='1'){
				alert("删除成功");
				toLoad();
			}else{
			alert("删除失败，请稍后重试");
			toLoad();
			}
		　　}
		});
}

/**
 * 打开电子病历列表
 */
function toCaseHistory(id){
	var $loadingToast = $('#loadingToast');
	if ($loadingToast.css('display') != 'none') {
		return;
	}
	$loadingToast.show();
	var url="./jsp/caseHistory/caseHistoryEdit.jsp?id="+id;
	window.location.href=url;
	$loadingToast.hide();
}


function toSysCaseHistory(id){
	var $loadingToast = $('#loadingToast');
	if ($loadingToast.css('display') != 'none') {
		return;
	}
	$loadingToast.show();
	var url="./jsp/caseHistory/sysCaseHistoryView.jsp?id="+id;
	window.location.href=url;
	$loadingToast.hide();
}

function toImgCaseHistory(id){
	var $loadingToast = $('#loadingToast');
	if ($loadingToast.css('display') != 'none') {
		return;
	}
	$loadingToast.show();
	var url="./jsp/caseHistory/imgCaseHistoryView.jsp?id="+id;
	window.location.href=url;
	$loadingToast.hide();
}

/**
 * 病历保存
 */
function toSaveCase(id){
	var isRepet=false;
	if(id==""){
		id=0;
	}
	 var formData=$('#form').serialize();
	 var  saveUrl="./caseHistory/tosave.spring?id="+id;
	 var caseList="./caseHistory/toList.spring";
	 var historyName=$("#historyName").val();
	 var secondaryPassword=$("#secondaryPassword").val();//二次访问密码
	 var birthday=$("#birthday").val();//生日
	 var profession=$("#profession").val();//职业
	 var sex=$("#sex").val();//性别
	 var maritalStatus=$("#maritalStatus").val();//婚姻
	 var phone=$("#phone").val();//联系方式
	 var allergicHistory=$("#allergicHistory").val();//过敏史
	 var previousHistory=$("#previousHistory").val();//既往史
	 var personalHistory=$("#personalHistory").val();//个人史
	 var familyHistory=$("#familyHistory").val();//家族史
	 var obstericalHistory=$("#obstericalHistory").val();//婚育史
	 var menstrualHistory=$("#menstrualHistory").val();//月经史
	 var presentIllness=$("#presentIllness").val();//   现病史
	 var physicalExamination=$("#physicalExamination").val();//体格检查结果
	 var accessoryExamination=$("#accessoryExamination").val();//辅助检结果
	 if(historyName==""){
		 alert("病历名称不能为空");
		 $("#historyName").focus();
		 return;
	 }else if(birthday==""){
		 alert("生日不能为空");
		 $("#birthday").focus();
		 return;
	 }else  if(sex==""){
		 alert("性别不能为空");
		 $("#sex").focus();
		 return;
	 }else if(maritalStatus==""){
		 alert("婚姻不能为空");
		 $("#maritalStatus").focus();
		 return;
	 }else if(phone==""){
		 alert("联系方式不能为空");
		 $("#phone").focus();
		 return;
	 }else  if(allergicHistory==""){
		 alert("过敏史不能为空");
		 $("#allergicHistory").focus();
		 return;
	 }else if(previousHistory==""){
		 alert("既往史不能为空");
		 $("#previousHistory").focus();
		 return;
	 }else if(personalHistory==""){
		 alert("个人史不能为空");
		 $("#personalHistory").focus();
		 return;
	 }else  if(familyHistory==""){
		 alert("家族史不能为空");
		 $("#familyHistory").focus();
		 return;
	 }else  if(obstericalHistory==""){
		 alert("婚育史不能为空");
		 $("#obstericalHistory").focus();
		 return;
	 }else  if(menstrualHistory==""){
		 if(sex==1){
			 alert("月经史不能为空");
			 $("#menstrualHistory").focus();
			 return;
		 }
		
	 }else  if(presentIllness==""){
		 alert("现病史不能为空");
		 $("#presentIllness").focus();
		 return;
	 }else if(secondaryPassword==""){
		 alert("访问密码不能为空");
		 $("#secondaryPassword").focus();
		 return;
	 }
	 isRepet=true;
	 var $loadingToast = $('#loadingToast');
		if ($loadingToast.css('display') != 'none') {
			return;
		}
		$loadingToast.show();
	 $.ajax({
		　　url : saveUrl,
		　　data : formData,
		　　type : "POST",
		　　success : function(data) {
		if(data[14]=="1"){
			setTimeout(function() {
				$loadingToast.hide();
				$("#toast").show();
				setTimeout(function() {
					$("#toast").hide();
					window.location.href=caseList;
				}, 1000);
			}, 1000);
			
		}else{
			alert("保存失败，请稍后保存");
		}
		　　},
		});
	
}
/**
 * 选择女时，出现月经史
 * @returns
 */
function selectMenstrual(){
	var sex=$("#sex").val();
	if(sex==1){//女
		$("#yuejing").show();
	}else{
		$("#yuejing").hide();
	}

}

/**
 * 新建病历
 * @returns
 */
function cteateCase(){
	var addurl="./caseHistory/addCase.spring";
	window.location.href=addurl;
}

function openbase(){
	var idchecked=$("#openbase").attr("checked");
	if(idchecked){
		$("#basehistory").show();
	}else{
		$("#basehistory").hide();
	}
	
}

function openimg(){
	var idchecked=$("#openimg").attr("checked");
	if(idchecked){
		$("#imghistory").show();
	}else{
		$("#imghistory").hide();
	}
	
}

function openinquiry(){ 
	var idchecked=$("#openinquiry").attr("checked");
	if(idchecked){
		$("#inquiryhistory").show();
	}else{
		$("#inquiryhistory").hide();
	}
	
}

